package com.learn;

import java.util.Arrays;

public class SelectSort {

    public static void main(String[] args) {
        int[] arrs = new int[]{4, 2, 6, 1, 8, 9, 5};
        selectSort(arrs);
        System.out.println(Arrays.toString(arrs));
    }

    public static void selectSort(int[] arrs) {
        int length = arrs.length;
        if (arrs == null || length < 2) {
            return;
        }

        for (int i = 0; i < length; i++) {
            int min_index = i;
            for (int j = i + 1; j < length; j++) {
                min_index = arrs[min_index] > arrs[j] ? j : min_index;
            }

            if (min_index != i) {
                int swap = arrs[min_index];
                arrs[min_index] = arrs[i];
                arrs[i] = swap;
            }
        }
    }
}
